package com.candoo.oil.scan;


import android.util.Log;
import android.widget.Toast;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.magicrf.uhfreaderlib.reader.Tools;
import com.magicrf.uhfreaderlib.reader.UhfReader;
/**
 * Created by Tinder on 2016/10/16.
 */

public class UhfClass {


    private static int _oid=0;
    public static String ReadTid(byte[] epc)
    {


        //String hex = Integer.toHexString(epc[0] & 255).toUpperCase();
        UhfClass u=new UhfClass();
        Log.e("epc","设置EPC_"+epc[0]);
        u.setEpc(Tools.Bytes2HexString(epc,epc.length));
        if(epc[0]!=-52)
        {//重写Epc
            Log.e("epc","重写EPC");
            _oid++;
            if(_oid>10000)
                _oid=1;
            ;String cc="CC"+ new SimpleDateFormat("yyyyMMddhhmmssSSS").format(new Date())+String.format("%05d",_oid);
            Log.e("o",cc);
            u.ReWriteEPC(cc,"00000000");
            return "";
        }
        Log.e("epc","读取tid");
        return u.getTid("00000000");


    }

    private UhfReader reader;
    private UhfReaderDevice readerDevice;

    private String epc;
    public String getEpc() {
        return epc;
    }
    /**
     * @param epc the epc to set
     */
    public void setEpc(String epc) {
        this.epc = epc;

        //获取读写器实例，若返回Null,则串口初始化失败
        reader = UhfReader.getInstance();
        //获取读写器设备示例，若返回null，则设备电源打开失败
        readerDevice = UhfReaderDevice.getInstance();
        if(reader == null){
            // Toast.makeText(getApplicationContext(), "serialport init fail", 0).show();
            return ;
        }
        if(readerDevice == null){
            // Toast.makeText(getApplicationContext(), "UHF reader power on failed", 0).show();
            return ;
        }

        reader.selectEpc(Tools.HexString2Bytes(epc));
    }



    public String  getTid(String pwd)//00000000
    {
        byte[] accessPassword = Tools.HexString2Bytes(pwd);
        //读取数据区数据
        byte[] data = reader.readFrom6C(2, 0, 6, accessPassword);
        if(data != null && data.length > 1){
            Log.e("epc",""+data.length);
            String dataStr =  Tools.Bytes2HexString(data, data.length);
            Log.e("epc","读取成功："+dataStr);
            return  dataStr;
//				Toast.makeText(getApplicationContext(),dataStr, 0).show();
            //Log.e("epc","读数据：" + dataStr + "\n");
        }else{
//				Toast.makeText(getApplicationContext(), "读数据失败", Toast.LENGTH_SHORT).show();
            if(data != null){
                Log.e("epc","读数据失败，错误码：" + (data[0]&0xff) + "\n");
                //return;
            }
            Log.e("epc","读数据失败，返回为空"  + "\n");
        }
        return "";
    }

    public void ReWriteEPC(String epc,String pwd)
    {
        byte[] accessPassword = Tools.HexString2Bytes(pwd);
        //String writeData = "012345678901234567890123";
        if(epc.length()%4 != 0){

            Log.e("epc", "写入数据的长度以字为单位，1word = 2bytes");
        }
        byte[] dataBytes = Tools.HexString2Bytes(epc);
        //dataLen = dataBytes/2 dataLen是以字为单位的
        boolean writeFlag = reader.writeTo6C(accessPassword, 1, 2, dataBytes.length/2, dataBytes);
        if(writeFlag){
            Log.e("epc","写数据成功！"  + "\n");
        }else{
            Log.e("epc","写数据失败！"  + "\n");
        }
    }
}

